<?php

namespace app\common\model\system;

use app\common\loader\ModelSystem;
use app\common\model\BaseModel;
use app\common\traits\ApiInterfaceTrait;
use app\manage\loader\ModelAdmin;
use yiqiniu\extend\facade\Arrays;

/**
 * 系统接口表
 * Class SystemApi
 * @package app\common\model\system
 */
class SystemApiModel extends BaseModel
{
    use ApiInterfaceTrait;

    //
    protected $name = 'system_api';

    // 设置字段信息
    protected $schema = [
        'id' => 'int',        //
        'title' => 'string',        // 接口名称
        'router_method' => 'string',        // 请求方式 GET/POST
        'page_action' => 'string',        // 路径地址
        'pid' => 'int',        // 父节点
        'sort' => 'int',        // 排序
        'auth' => 'int',        // 认证级别 1.开放 2:登录 3认证
        'group_id' => 'int',        // 分组ID
        'type_id' => 'int',        // 0:权限,1:功能,2:目录
    ];

    /**
     * 角色管理权限选择
     */
    public function actions($roleId)
    {
        $apis = $this->db()
            ->where('title', '>', '')
            ->field('id,pid,child,title,module,page_action,module,router_method,type_id')
            ->order('sort desc')->select();


        $checkedActions = [];
        if (!empty($roleId) && $roleId > 0) {

            $checked = $this->db('system_roles_actions')
                ->where('role_id', $roleId)->select();
            if (!empty($checked)) {
                $checkedActions = array_column($checked, 'action_method');
            }

        }
        foreach ($apis as &$api) {
            if ($api['type_id'] === 0) {
                $api['checked'] = in_array($api['page_action'], $checkedActions, true);
            }
        }

        $data = Arrays::toTree($apis, 'id', 'pid');
        $info = ModelSystem::SystemRoles()->where('id', $roleId)->field('id,title,data_permission,description')->find();
        return [
            'id' => $info['id'] ?? 0,
            'title' => $info['title'] ?? '',
            'data_permission' => $info['data_permission'] ?? '',
            'description' => $info['description'] ?? '',
            'menus' => $data,
        ];

    }

    /**
     * 接口列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getActions()
    {
        return $this->db()->where('type_id', 2)
            ->whereOr(function ($query) {
                $query->where('type_id', '<', 2)->where('pid', '>', 0)->where('title', '>', '');
            })->order('sort desc')->field('id,pid,title,type_id,page_action')->select();
    }


    /**
     * 角色管理权限
     */
    public function getActionsData($group_id)
    {
        if ($group_id > 0) {
            $where = ['role_id' => $group_id];
            $action =  ModelSystem::SystemRolesActions()->where($where)->column(null,'action_method');
        }else {
            $action =  ModelSystem::SystemApi()->where('title','>' , 1)
                ->where('page_action' , '>' , 1)
                ->column(null,'page_action');
        }

        $apis = $this->db('system_menu')
            ->where('is_show', 1)->json(['front'])
            ->field('id,title,pid,front,page_action,icon,child,depth');
        $db = $apis->where(function ($query) use ($action) {
            $query->whereIn(true ? 'page_action' : 'id', $action)->whereOr('pid', 0);
        });
       $apis = $db->order('order_id desc')->select();

        if (empty($apis)){
            return [];
        }
       return Arrays::toTree($apis, 'id', 'pid');

    }
}
